package com.zhku.meeting.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhku.meeting.entities.Department;
import com.zhku.meeting.entities.MeetingRecord;
import com.zhku.meeting.entities.Msg;
import com.zhku.meeting.mapper.MeetingRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

@Service
public class MeetingRecordService {

    @Autowired
    private MeetingRecordMapper meetingRecordMapper;

//    public Msg getHistory(Map<String, Object> para) {
//        Integer pass = Integer.parseInt((String) para.get("pass"));
//        long a = System.currentTimeMillis();
//        Date meetingDate =new Date(a);
//        String name = null;
//        if(para.containsKey("name")){
//            name = (String)para.get("name");
//        }
//        String roomNo =null;
//        if (para.containsKey("roomNo")){
//            roomNo = (String) para.get("roomNo");
//        }
//        Integer meetSlot =null;
//        if (para.containsKey("meetSlot")){
//            meetSlot = Integer.parseInt((String) para.get("meetSlot"));
//        }
//        Integer pageSize = Integer.parseInt((String) para.get("pageSize"));
//        Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
//        MeetingRecord meetingRecord = new MeetingRecord();
//        meetingRecord.setName(name);
//        meetingRecord.setMeetingDate(meetingDate);
//        meetingRecord.setRoomNo(roomNo);
//        meetingRecord.setMeetingSlot(meetSlot);
//        PageHelper.startPage(currentPage,pageSize);
//        List<MeetingRecord> meetingRecords = meetingRecordMapper.selectByCondition(meetingRecord,pass);
//        PageInfo pageInfo = new PageInfo(meetingRecords);
//        Map<String,Object> map = new HashMap<>();
//        map.put("result",pageInfo);
//        return Msg.success().add(map);
//    }

    public void addRecord(MeetingRecord meetingRecord) {
        meetingRecordMapper.insert(meetingRecord);
    }

//    public Msg getMeetingRecord(Map<String, Object> para) {
//        Integer recordId = Integer.parseInt((String) para.get("recordId"));
//        MeetingRecord meetingRecord = meetingRecordMapper.selectByPrimaryKey(recordId);
//        LocalDate now = LocalDate.now();
//        Date date = meetingRecord.getMeetingDate();
//        LocalDate date1 = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
//        if (date1.isBefore(now)){
//            meetingRecord.setStatus("未开始");
//        }else{
//            meetingRecord.setStatus("已开始");
//        }
//        Map<String,Object> map = new HashMap<>();
//        map.put("result",meetingRecord);
//        return Msg.success().add(map);
//    }

    public Msg getAll(Map<String, Object> para) {
        List<MeetingRecord> meetingRecords;
        Map<String,Object> map = new HashMap<>();
        if(para.containsKey("currentPage")){
            Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
            Integer pageSize = Integer.parseInt((String)para.get("pageSize"));
            PageHelper.startPage(currentPage,pageSize);
            meetingRecords = meetingRecordMapper.selectAll();
            for (MeetingRecord m: meetingRecords){
                LocalDate now = LocalDate.now();
                Date date = m.getMeetingDate();
                LocalDate date1 = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                if (date1.isBefore(now)){
                    m.setStatus("已过期");
                }else{
                    m.setStatus("未过期");
                }
            }
            PageInfo pageInfo = new PageInfo(meetingRecords);
            map.put("result",pageInfo);
        }else{
            meetingRecords= meetingRecordMapper.selectAll();
            for (MeetingRecord m: meetingRecords){
                LocalDate now = LocalDate.now();
                Date date = m.getMeetingDate();
                LocalDate date1 = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                if (date1.isBefore(now)){
                    m.setStatus("已过期");
                }else{
                    m.setStatus("未过期");
                }
            }
            map.put("result",meetingRecords);
        }
        return Msg.success().add(map);
    }

    public Msg getMeetingRecordCondition(Map<String, Object> para) {
        Integer pass = Integer.parseInt((String) para.get("pass"));
        long a = System.currentTimeMillis();
        Date meetingDate =new Date(a);
        String name = null;
        if(para.containsKey("name")){
            name = (String)para.get("name");
        }
        String roomNo =null;
        if (para.containsKey("roomNo")){
            roomNo = (String) para.get("roomNo");
        }
        Integer meetSlot =null;
        if (para.containsKey("meetSlot")){
            meetSlot = Integer.parseInt((String) para.get("meetSlot"));
        }
        Integer pageSize = Integer.parseInt((String) para.get("pageSize"));
        Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
        MeetingRecord meetingRecord = new MeetingRecord();
        meetingRecord.setName(name);
        meetingRecord.setMeetingDate(meetingDate);
        meetingRecord.setRoomNo(roomNo);
        meetingRecord.setMeetingSlot(meetSlot);
        PageHelper.startPage(currentPage,pageSize);
        List<MeetingRecord> meetingRecords = meetingRecordMapper.selectByCondition(meetingRecord,pass);
        PageInfo pageInfo = new PageInfo(meetingRecords);
        Map<String,Object> map = new HashMap<>();
        map.put("result",pageInfo);
        return Msg.success().add(map);
    }


    public void revoke(Integer applyId) {
        meetingRecordMapper.deleteByPrimaryKey(applyId);
    }
//    @Autowired
//    private MeetingRecordDao meetingRecordDao;
//    public MeetingRecord getRecord(Integer id){
//        return meetingRecordDao.findById(id).get();
//    }
}
